package ro.msg.office.beans;


import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlInputTextarea;
import javax.faces.context.FacesContext;
import ro.msg.office.ticketmanager.DO.TicketDO;
import ro.msg.office.ticketmanager.back.ifc.TicketManager;


/**
 * @author nikoran
 * This class is created to represent the managed bean for the detailsUser.xhtml page.
 * It contains the details of the ticket, which will be displayed on the above page.
 */
public class DetailsUserBean {
	
	/**
	 * This is a string variable, containing the start time's hours and minutes.
	 */
	private String startTimeString;
	
	/**
	 * This is a string variable, containing the end time's hours and minutes.
	 */
	private String endTimeString;
	
	/**
	 * This is a representation of the InputTextarea from the detailsUser.xhtml page.
	 */
	private HtmlInputTextarea updatedDescription;

	/**
	 * This is the ticket chosen from the datatable, which will be displayed on the detailsUser.xhtml page.
	 */
	private static TicketDO ticketDO;
    
	/**
	 * This is an interface of the TicketManagerBean, which contains the methods used to save/update a ticket.
	 */
	@EJB
    TicketManager ticketEJB;
	
    
	

	/**
	 * @return - This returns the chosen ticket from the datatable.
	 */
	public TicketDO getTicketDO() {
		return ticketDO;
	}

	/**
	 * @param ticket - This sets the value of the ticket to be the given parameter.
	 */
	public static void setTicketDO(TicketDO ticket) {
		ticketDO = ticket;
	}
	
	/**
	 * @return - This returns the representation of the InputTextarea from the detailsUser.xhtml page.
	 */
	public HtmlInputTextarea getUpdatedDescription() {
		return updatedDescription;
	}

	/**
	 * @param updatedDescription - This sets the representation of the InputTextarea to have the parameter value.
	 */
	public void setUpdatedDescription(HtmlInputTextarea updatedDescription) {
		this.updatedDescription = updatedDescription;
	}
	
	/**
	 * @return - This returns from the start time the hours and minutes.
	 */
	public String getStartTimeString() {
		
	  if(LoginBean.getSigned())
	  {
		Integer stime1 = (Integer) ticketDO.getStartTime().getHours();
		Integer stime2 = (Integer) ticketDO.getStartTime().getMinutes();
		setStartTimeString(stime1.toString() + ":" + stime2.toString());
	  }
		return startTimeString;
	}

	/**
	 * @return - This returns from the end time the hours and minutes.
	 */
	public String getEndTimeString() {
		
	  if(LoginBean.getSigned())
	  {
		Integer etime1 = (Integer) ticketDO.getEndTime().getHours();
		Integer etime2 = (Integer) ticketDO.getEndTime().getMinutes();
		setEndTimeString(etime1.toString() + ":" + etime2.toString());
	  }
		return endTimeString;
	}

	/**
	 * @param startTimeString - Sets the start time value to be the given parameter.
	 */
	public void setStartTimeString(String startTimeString) {
		this.startTimeString = startTimeString;
	}

	/**
	 * @param endTimeString - Sets the end time value to be the given parameter.
	 */
	public void setEndTimeString(String endTimeString) {
		this.endTimeString = endTimeString;
	}
	

	/**
	 * This method will be called when the "ReSend Button" of the detailsUser.xhtml page will be pressed.
	 * The description of the ticket will be changed and the ticket will be resent.
	 * If the ticket is resent successfully a message will appear: "Tickets is Sent!!!".
	 */
	public void updateTicket(){

		String description = (String) updatedDescription.getValue();
		
		TicketDO ticket = new TicketDO();
		ticket.setTicketId(ticketDO.getTicketId());
		ticket.setDescription(description);
		ticket.setStatus(new Long(1));
		ticket.setCreationDate(new Date());
		
		ticketEJB.updateTicket(ticket);
		
        FacesMessage message = new FacesMessage("Tickets is Sent!!!");
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, message);
		
	}

	/**
	 * This method will be called when the "Save Button" of the detailsUser.xhtml page will be pressed.
	 * The description of the ticket will be changed and will be saved as "Draft".
	 * If the ticket is saved successfully a message will appear: "Draft Saved!!!".
	 */
	public void saveTicket(){
		
		String description = (String) updatedDescription.getValue();
		
		TicketDO ticket = new TicketDO();
		ticket.setTicketId(ticketDO.getTicketId());
		ticket.setDescription(description);
		ticket.setStatus(ticketDO.getStatus());
		ticket.setCreationDate(new Date());
		
		ticketEJB.updateTicket(ticket);
		
        FacesMessage message = new FacesMessage("Draft Saved!!!");
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, message);
		
	}
	
}
